package cn.ondraw.ragflow.controller;

import cn.ondraw.ragflow.pojo.request.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import cn.ondraw.common.core.domain.R;
import cn.ondraw.ragflow.pojo.request.*;
import cn.ondraw.ragflow.pojo.request.*;
import cn.ondraw.ragflow.service.SessionRagFlowService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

/**
 * 聊天会话控制器
 */
@RestController
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/v1/chats")
public class SessionRagFlowController {

    private final SessionRagFlowService sessionRagFlowService;

    /**
     * 创建会话
     *
     * @param chatId  关联的聊天助手 ID
     * @param request 创建会话请求对象
     * @return 创建会话响应对象
     */
    @PostMapping("/{chat_id}/sessions")
    public R<Object> createSession(@PathVariable("chat_id") String chatId, @RequestBody CreateSessionRequest request) {
        return R.ok(sessionRagFlowService.createSession(chatId, request));
    }

    /**
     * 更新会话
     *
     * @param chatId    关联的聊天助手 ID
     * @param sessionId 要更新的会话 ID
     * @param request   更新会话请求对象
     * @return 更新会话响应对象
     */
    @PutMapping("/{chat_id}/sessions/{session_id}")
    public R<Object> updateSession(@PathVariable("chat_id") String chatId, @PathVariable("session_id") String sessionId, @RequestBody UpdateSessionRequest request) {
        return R.ok(sessionRagFlowService.updateSession(chatId, sessionId, request));
    }

    /**
     * 列出会话
     *
     * @param request 列出会话请求参数对象
     * @return 列出会话响应对象
     */
    @GetMapping("/{chat_id}/sessions")
    public R<Object> listSessions(@ModelAttribute ListSessionsRequest request, @PathVariable("chat_id") String chatId) {
        request.setChat_id(chatId);
        return R.ok(sessionRagFlowService.listSessions(request));
    }

    /**
     * 删除会话
     *
     * @param chatId  关联的聊天助手 ID
     * @param request 删除会话请求对象
     * @return 删除会话响应对象
     */
    @DeleteMapping("/{chat_id}/sessions")
    public R<Object> deleteSessions(@PathVariable("chat_id") String chatId, @RequestBody DeleteSessionsRequest request) {
        return R.ok(sessionRagFlowService.deleteSessions(chatId, request));
    }

    /**
     * 与聊天助手对话
     *
     * @param chatId  关联的聊天助手 ID
     * @param request 对话请求对象
     * @return 对话响应对象
     */
    @PostMapping("/{chat_id}/completions")
    public SseEmitter converseWithAssistant(@PathVariable("chat_id") String chatId, @RequestBody ConverseRequest request) {
        return sessionRagFlowService.converseWithAssistant(chatId, request);
    }

    /**
     * 创建与代理的会话
     *
     * @param agentId 关联的代理 ID
     * @param request 创建代理会话请求对象
     * @return 创建代理会话响应对象
     */
    @PostMapping("/agents/{agent_id}/sessions")
    public R<Object> createAgentSession(@PathVariable("agent_id") String agentId, @RequestBody CreateAgentSessionRequest request) {
        return R.ok(sessionRagFlowService.createAgentSession(agentId, request));
    }

    /**
     * 与代理对话
     *
     * @param agentId 关联的代理 ID
     * @param request 与代理对话请求对象
     * @return 与代理对话响应对象
     */
    @PostMapping("/agents/{agent_id}/completions")
    public SseEmitter converseWithAgent(@PathVariable("agent_id") String agentId, @RequestBody ConverseWithAgentRequest request) {
        return sessionRagFlowService.converseWithAgent(agentId, request);
    }

    /**
     * 列出代理会话
     *
     * @param request 列出代理会话请求参数对象
     * @return 列出代理会话响应对象
     */
    @GetMapping("/agents/{agent_id}/sessions")
    public R<Object> listAgentSessions(@ModelAttribute ListAgentSessionsRequest request) {
        return R.ok(sessionRagFlowService.listAgentSessions(request));
    }

    /**
     * 删除代理会话
     *
     * @param agentId 关联的代理 ID
     * @param request 删除代理会话请求对象
     * @return 删除代理会话响应对象
     */
    @DeleteMapping("/agents/{agent_id}/sessions")
    public R<Object> deleteAgentSessions(@PathVariable("agent_id") String agentId, @RequestBody DeleteAgentSessionsRequest request) {
        return R.ok(sessionRagFlowService.deleteAgentSessions(agentId, request));
    }

    /**
     * 生成相关问题
     *
     * @param request 生成相关问题请求对象
     * @return 生成相关问题响应对象
     */
    @PostMapping("/conversation/related_questions")
    public R<Object> generateRelatedQuestions(@RequestBody RelatedQuestionsRequest request) {
        return R.ok(sessionRagFlowService.generateRelatedQuestions(request));
    }
}
